%% Training and Testing Single Action Recognition in the Weizmann Dataset
%Using Optical Flow Features

clear all
close all
clc

%%AQUI VOY... no funciona no se pq... revisar codigo en C++ y luego el
%%codigo en Matlab
%Estoy prbando ahora con Euclidean Distance Transform Features.



%NICTA && Server
addpath('/home/johanna/toolbox/libsvm-3.20/matlab')
addpath('/home/johanna/toolbox/yael/matlab');

%Home
addpath('/media/johanna/HD1T/Toolbox/libsvm-3.20/matlab');
addpath('/media/johanna/HD1T/Toolbox/yael/matlab');


% Si cambias de Ng, correr los sgtes. dos archivos:
Ncent = 256;
display('Calculating FV for Training');
FV_weizmann_training(Ncent);
display('Calculating FV for Testing');
FV_weizmann_testing(Ncent);

Ng = int2str(Ncent);


ACC = [];
all_prediction = [];
all_real = [];
for r=1:9
run = int2str(r);
%fprintf('Running for %s \n', run);
actionNames = importdata('actionNames.txt');

people_train = importdata(strcat('./run', run, '/train_list_run', run, '.dat'));
people_test = importdata(strcat('./run', run, '/test_list_run', run, '.dat'));

n_pe_tr  = length(people_train);
n_pe_te  = length(people_test);
n_actions = length(actionNames);

data_train = [];
labels_train = [];

%% TRAINING
%  Loading Training data
display('TRAINING');
fprintf('Running for %s \n', run);
    for pe = 1: n_pe_tr
        for act = 1:n_actions
            load_name = strcat('./run', run,  '/FV_training/FV_', people_train(pe),'_',actionNames(act), '_Ng', Ng, '.txt');
            sLoad = char(load_name);
            FV = load(sLoad);
            data_train = [data_train FV];
            labels_train = [labels_train (act)];
        end
    end
data_train = data_train';
labels_train = labels_train';
model = svmtrain(labels_train, data_train, ['-s 0 -t 0 -b 1' ]);

%% TESTING. This testing is only for single actions
%  Loading Testing data
display('TESTING');
fprintf('Running for %s \n', run);
data_test = [];
labels_test = [];
for pe = 1: n_pe_te
        for act = 1:n_actions
            load_name = strcat('./run', run,  '/FV_testing/FV_', people_test(pe),'_',actionNames(act), '_Ng', Ng, '.txt');
            sLoad = char(load_name);
            FV = load(sLoad);
            %hist(FV)
            %pause
            data_test = [data_test FV];
            labels_test = [labels_test (act)];
        end
end


data_test = data_test';
labels_test = labels_test';


[predicted_label, accuracy, prob_estimates] = svmpredict(labels_test, data_test, model, ['-b 1']);
all_prediction = [all_prediction; predicted_label ];
all_real = [all_real; labels_test];
ACC = [ACC accuracy(1,1)];
end

